import request from '@/utils/request.js'
import { useTokenStore } from '@/stores/token.js'
//课程分类列表查询
export const lessonCategoryListService = ()=>{
    
    //在pinia中定义的响应式数据,都不需要.value
    return request.get('/category')
}

//课程分类添加
export const lessonCategoryAddService = (categoryData)=>{
    return request.post('/category',categoryData)
}

//课程分类修改
export const lessonCategoryUpdateService = (categoryData)=>{
    return  request.put('/category',categoryData)
 }

 //课程分类删除
export const lessonCategoryDeleteService = (id)=>{
    return request.delete('/category', {
        params: { id }
    })
}

//课程分类详情查询
export const lessonCategoryDetailService = (id) => {
    return request.get('/category/detail', {
        params: { id }
    })
}

//课程列表查询
export const lessonListService = (params)=>{
    // 确保分页参数存在
    const pageNum = params.pageNum || 1;
    const pageSize = params.pageSize || 10;
    const start = (pageNum - 1) * pageSize;
    
    return request.get('/lesson', {
        params: {
            ...params,
            pageNum,
            pageSize,
            start
        }
    })
}

//获取我的课程列表
export const myLessonListService = (params)=>{
    // 确保分页参数存在
    const pageNum = params.pageNum || 1;
    const pageSize = params.pageSize || 10;
    const start = (pageNum - 1) * pageSize;
    
    return request.get('/lesson', {
        params: {
            ...params,
            pageNum,
            pageSize,
            start
        }
    })
}

 //课程添加
export const lessonAddService = (lessonData)=>{
    return request.post('/lesson',lessonData);

}

//课程删除
export const lessonDeleteService = (id)=>{
    return request.delete('/lesson', {
        params: { id }
    })
}

//课程更新
export const lessonUpdateService = (lessonData)=>{
    return request.put('/lesson', lessonData)
}

// 点赞课程
export const lessonLikeService = (lessonId) => {
    return request.post('/lesson/like', { lessonId })
}

// 取消点赞课程
export const lessonUnlikeService = (lessonId) => {
    return request.delete('/lesson/like', {
        params: { lessonId }
    })
}

// 收藏课程
export const lessonFavoriteService = (lessonId) => {
    return request.post('/lesson/favorite', { lessonId })
}

// 取消收藏课程
export const lessonUnfavoriteService = (lessonId) => {
    return request.delete('/lesson/favorite', {
        params: { lessonId }
    })
}

// 获取课程点赞状态
export const getLessonLikeStatusService = (lessonId) => {
    return request.get('/lesson/like/status', {
        params: { lessonId }
    })
}

// 获取课程收藏状态
export const getLessonFavoriteStatusService = (lessonId) => {
    return request.get('/lesson/favorite/status', {
        params: { lessonId }
    })
}

// 获取课程点赞数量
export const getLessonLikeCountService = (lessonId) => {
    return request.get('/lesson/like/count', {
        params: { lessonId }
    })
}

// 获取课程收藏数量
export const getLessonFavoriteCountService = (lessonId) => {
    return request.get('/lesson/favorite/count', {
        params: { lessonId }
    })
}

// 获取用户收藏的课程列表
export const getUserFavoritesService = () => {
    return request.get('/lesson/favorite/list')
}

// 获取推荐课程列表
export const getRecommendedLessonsService = (limit = 6) => {
    return request.get('/lesson/recommend', {
        params: { limit }
    })
}

// 管理员获取所有课程列表
export const adminLessonListService = (params) => {
    // 确保分页参数存在
    const pageNum = params.pageNum || 1;
    const pageSize = params.pageSize || 10;
    const start = (pageNum - 1) * pageSize;
    
    return request.get('/lesson/admin/list', {
        params: {
            ...params,
            pageNum,
            pageSize,
            start
        }
    })
}

// 管理员更新课程状态
export const adminUpdateLessonStateService = (id, state) => {
    return request.patch('/lesson/admin/state', null, {
        params: {
            id,
            state
        }
    })
}

